package com.supreme.cacheserver.utils;

import org.redisson.api.*;
import org.redisson.api.listener.MessageListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;

@Component
public class RedissonType {

    @Resource
    private RedissonClient redissonClient;

    /**
     * 原子整长形
     * @param key
     * @return
     */
    public RAtomicLong getAtomicLong(String key) {
        return redissonClient.getAtomicLong(key);
    }

    /**
     * 原子双精度浮点
     * @param key
     * @return
     */
    public RAtomicDouble getAtomicDouble(String key) {
        return redissonClient.getAtomicDouble(key);
    }

    /**
     * 分布式集合
     * @param key
     * @return
     */
    public RMap<String, Object> getMap(String key) {
        return redissonClient.getMap(key);
    }

    /**
     * 通用对象桶
     * @param key
     * @return
     */
    public RBucket<Object> getBucket(String key) {
        return redissonClient.getBucket(key);
    }

    public Map<String, Object> getBuckets(String ...keys) {
        RBuckets buckets = redissonClient.getBuckets();
        return buckets.get(keys);
    }

    /**
     * 二进制流
     * @param key
     * @return
     */
    public RBinaryStream getBinStream(String key) {
        return redissonClient.getBinaryStream(key);
    }

    /**
     * 地理空间对象桶
     * @param key
     * @return
     */
    public RGeo<Object> getGeospatialBucket(String key) {
        return redissonClient.getGeo(key);
    }

    /**
     * BitSet
     * @param key
     * @return
     */
    public RBitSet getBitSet(String key) {
        return redissonClient.getBitSet(key);
    }

    /**
     * BitSet数据分片
     */
//    public RClusteredBitSet getClusteredBitSet(String key) {
//        return redissonClient.getClusteredBitSet(key);
//    }

    /**
     * 话题（订阅分发）
     * @param key
     * @return
     */
    public RTopic getTopic(String key) {
        return redissonClient.getTopic(key);
    }

    public int addTopicListener(RTopic topic, Class<String> clazz) {
        return topic.addListener(clazz, new MessageListener<String>() {
            @Override
            public void onMessage(CharSequence charSequence, String s) {

            }
        });
    }

    public long publishTopicMessage(RTopic topic, String message) {
        return topic.publish(message);
    }

    /**
     * 模糊话题
     * @param key = "topic1.*"
     * @return
     */
    public RPatternTopic getPatternTopic(String key) {
        return redissonClient.getPatternTopic(key);
    }

    /**
     * 布隆过滤器
     * @param key
     * @return
     */
    public RBloomFilter<Object> getBloomFilter(String key) {
        return redissonClient.getBloomFilter(key);
    }

    /**
     * 布隆过滤器数据分片
     */
//    public RClusteredBloomFilter getClusteredBloomFilter(String key) {
//        return redissonClient.getClusteredBloomFilter("sample");
//    }

    /**
     * 基数估计算法
     * @param key
     * @return
     */
    public RHyperLogLog<Object> getHyperLogLog(String key) {
        return redissonClient.getHyperLogLog(key);
    }

    /**
     * 整长型累加器
     * @param key
     * @return
     */
    public RLongAdder getLongAdder(String key) {
        return redissonClient.getLongAdder(key);
    }

    /**
     * 双精度浮点累加器
     * @param key
     * @return
     */
    public RDoubleAdder getDoubleAdder(String key) {
        return redissonClient.getDoubleAdder(key);
    }

    /**
     * 限流器
     * @param key
     * @return
     */
    public RRateLimiter getRateLimiter(String key) {
        return redissonClient.getRateLimiter(key);
    }
}
